package com.wuxu.huanfu

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.wuxu.huanfu.R


/**
 *@author wuxu
 *@date 2024/5/16 18:34
 *@description 换肤测试页面
 */
class SplashActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_skin)

        if (checkAndRequestPermissions(this)){
            this.startActivity(Intent(this,HFActivity::class.java))
        }

    }


    val REQUEST_CODE = 1
    private fun checkAndRequestPermissions(activity: Activity): Boolean {
        val permissionRead = ContextCompat.checkSelfPermission(
            activity,
            Manifest.permission.READ_EXTERNAL_STORAGE
        )
        val permissionWrite = ContextCompat.checkSelfPermission(
            activity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
        if (permissionRead != PackageManager.PERMISSION_GRANTED ||
            permissionWrite != PackageManager.PERMISSION_GRANTED
        ) {
            // 权限没有被授予，我们应该请求它们
            ActivityCompat.requestPermissions(
                activity, arrayOf<String>(
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                ),
                REQUEST_CODE
            )
            return false
        }
        return true
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            REQUEST_CODE -> {
                if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 权限被授予，可以进行文件操作
                    this.startActivity(Intent(this,HFActivity::class.java))
                } else {
                    // 权限被拒绝，可以向用户解释为什么需要这些权限
                }
            }
        }
    }
}